{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 玖零猴的Demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import SimpleITK as sitk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "flair_name = \"_flair.nii.gz\"\n",
    "t1_name = \"_t1.nii.gz\"\n",
    "t1ce_name = \"_t1ce.nii.gz\"\n",
    "t2_name = \"_t2.nii.gz\"\n",
    "mask_name = \"_seg.nii.gz\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "bratshgg_path = r\"D:\\Project\\CollegeDesign\\dataset\\2-MICCAI_BraTS_2018\\MICCAI_BraTS_2018_Data_Training\\HGG\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "bratslgg_path = r\"D:\\Project\\CollegeDesign\\dataset\\2-MICCAI_BraTS_2018\\MICCAI_BraTS_2018_Data_Training\\LGG\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "outputImg_path = r\".\\trainImage\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "outputMask_path = r\".\\trainMask\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not os.path.exists(outputImg_path):\n",
    "    os.mkdir(outputImg_path)\n",
    "if not os.path.exists(outputMask_path):\n",
    "    os.mkdir(outputMask_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def file_name_path(file_dir, dir=True, file=False):\n",
    "    \"\"\"\n",
    "    get root path,sub_dirs,all_sub_files\n",
    "    :param file_dir:\n",
    "    :return: dir or file\n",
    "    \"\"\"\n",
    "    for root, dirs, files in os.walk(file_dir):\n",
    "        if len(dirs) and dir:\n",
    "            print(\"sub_dirs:\", dirs)\n",
    "            return dirs\n",
    "        if len(files) and file:\n",
    "            print(\"files:\", files)\n",
    "            return files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sub_dirs: ['Brats18_2013_10_1', 'Brats18_2013_11_1', 'Brats18_2013_12_1', 'Brats18_2013_13_1', 'Brats18_2013_14_1', 'Brats18_2013_17_1', 'Brats18_2013_18_1', 'Brats18_2013_19_1', 'Brats18_2013_20_1', 'Brats18_2013_21_1', 'Brats18_2013_22_1', 'Brats18_2013_23_1', 'Brats18_2013_25_1', 'Brats18_2013_26_1', 'Brats18_2013_27_1', 'Brats18_2013_2_1', 'Brats18_2013_3_1', 'Brats18_2013_4_1', 'Brats18_2013_5_1', 'Brats18_2013_7_1', 'Brats18_CBICA_AAB_1', 'Brats18_CBICA_AAG_1', 'Brats18_CBICA_AAL_1', 'Brats18_CBICA_AAP_1', 'Brats18_CBICA_ABB_1', 'Brats18_CBICA_ABE_1', 'Brats18_CBICA_ABM_1', 'Brats18_CBICA_ABN_1', 'Brats18_CBICA_ABO_1', 'Brats18_CBICA_ABY_1', 'Brats18_CBICA_ALN_1', 'Brats18_CBICA_ALU_1', 'Brats18_CBICA_ALX_1', 'Brats18_CBICA_AME_1', 'Brats18_CBICA_AMH_1', 'Brats18_CBICA_ANG_1', 'Brats18_CBICA_ANI_1', 'Brats18_CBICA_ANP_1', 'Brats18_CBICA_ANZ_1', 'Brats18_CBICA_AOD_1', 'Brats18_CBICA_AOH_1', 'Brats18_CBICA_AOO_1', 'Brats18_CBICA_AOP_1', 'Brats18_CBICA_AOZ_1', 'Brats18_CBICA_APR_1', 'Brats18_CBICA_APY_1', 'Brats18_CBICA_APZ_1', 'Brats18_CBICA_AQA_1', 'Brats18_CBICA_AQD_1', 'Brats18_CBICA_AQG_1', 'Brats18_CBICA_AQJ_1', 'Brats18_CBICA_AQN_1', 'Brats18_CBICA_AQO_1', 'Brats18_CBICA_AQP_1', 'Brats18_CBICA_AQQ_1', 'Brats18_CBICA_AQR_1', 'Brats18_CBICA_AQT_1', 'Brats18_CBICA_AQU_1', 'Brats18_CBICA_AQV_1', 'Brats18_CBICA_AQY_1', 'Brats18_CBICA_AQZ_1', 'Brats18_CBICA_ARF_1', 'Brats18_CBICA_ARW_1', 'Brats18_CBICA_ARZ_1', 'Brats18_CBICA_ASA_1', 'Brats18_CBICA_ASE_1', 'Brats18_CBICA_ASG_1', 'Brats18_CBICA_ASH_1', 'Brats18_CBICA_ASK_1', 'Brats18_CBICA_ASN_1', 'Brats18_CBICA_ASO_1', 'Brats18_CBICA_ASU_1', 'Brats18_CBICA_ASV_1', 'Brats18_CBICA_ASW_1', 'Brats18_CBICA_ASY_1', 'Brats18_CBICA_ATB_1', 'Brats18_CBICA_ATD_1', 'Brats18_CBICA_ATF_1', 'Brats18_CBICA_ATP_1', 'Brats18_CBICA_ATV_1', 'Brats18_CBICA_ATX_1', 'Brats18_CBICA_AUN_1', 'Brats18_CBICA_AUQ_1', 'Brats18_CBICA_AUR_1', 'Brats18_CBICA_AVG_1', 'Brats18_CBICA_AVJ_1', 'Brats18_CBICA_AVV_1', 'Brats18_CBICA_AWG_1', 'Brats18_CBICA_AWH_1', 'Brats18_CBICA_AWI_1', 'Brats18_CBICA_AXJ_1', 'Brats18_CBICA_AXL_1', 'Brats18_CBICA_AXM_1', 'Brats18_CBICA_AXN_1', 'Brats18_CBICA_AXO_1', 'Brats18_CBICA_AXQ_1', 'Brats18_CBICA_AXW_1', 'Brats18_CBICA_AYA_1', 'Brats18_CBICA_AYI_1', 'Brats18_CBICA_AYU_1', 'Brats18_CBICA_AYW_1', 'Brats18_CBICA_AZD_1', 'Brats18_CBICA_AZH_1', 'Brats18_CBICA_BFB_1', 'Brats18_CBICA_BFP_1', 'Brats18_CBICA_BHB_1', 'Brats18_CBICA_BHK_1', 'Brats18_CBICA_BHM_1', 'Brats18_TCIA01_131_1', 'Brats18_TCIA01_147_1', 'Brats18_TCIA01_150_1', 'Brats18_TCIA01_180_1', 'Brats18_TCIA01_186_1', 'Brats18_TCIA01_190_1', 'Brats18_TCIA01_201_1', 'Brats18_TCIA01_203_1', 'Brats18_TCIA01_221_1', 'Brats18_TCIA01_231_1', 'Brats18_TCIA01_235_1', 'Brats18_TCIA01_335_1', 'Brats18_TCIA01_378_1', 'Brats18_TCIA01_390_1', 'Brats18_TCIA01_401_1', 'Brats18_TCIA01_411_1', 'Brats18_TCIA01_412_1', 'Brats18_TCIA01_425_1', 'Brats18_TCIA01_429_1', 'Brats18_TCIA01_448_1', 'Brats18_TCIA01_460_1', 'Brats18_TCIA01_499_1', 'Brats18_TCIA02_117_1', 'Brats18_TCIA02_118_1', 'Brats18_TCIA02_135_1', 'Brats18_TCIA02_151_1', 'Brats18_TCIA02_168_1', 'Brats18_TCIA02_171_1', 'Brats18_TCIA02_179_1', 'Brats18_TCIA02_198_1', 'Brats18_TCIA02_208_1', 'Brats18_TCIA02_222_1', 'Brats18_TCIA02_226_1', 'Brats18_TCIA02_274_1', 'Brats18_TCIA02_283_1', 'Brats18_TCIA02_290_1', 'Brats18_TCIA02_300_1', 'Brats18_TCIA02_309_1', 'Brats18_TCIA02_314_1', 'Brats18_TCIA02_321_1', 'Brats18_TCIA02_322_1', 'Brats18_TCIA02_331_1', 'Brats18_TCIA02_368_1', 'Brats18_TCIA02_370_1', 'Brats18_TCIA02_374_1', 'Brats18_TCIA02_377_1', 'Brats18_TCIA02_394_1', 'Brats18_TCIA02_430_1', 'Brats18_TCIA02_455_1', 'Brats18_TCIA02_471_1', 'Brats18_TCIA02_473_1', 'Brats18_TCIA02_491_1', 'Brats18_TCIA02_605_1', 'Brats18_TCIA02_606_1', 'Brats18_TCIA02_607_1', 'Brats18_TCIA02_608_1', 'Brats18_TCIA03_121_1', 'Brats18_TCIA03_133_1', 'Brats18_TCIA03_138_1', 'Brats18_TCIA03_199_1', 'Brats18_TCIA03_257_1', 'Brats18_TCIA03_265_1', 'Brats18_TCIA03_296_1', 'Brats18_TCIA03_338_1', 'Brats18_TCIA03_375_1', 'Brats18_TCIA03_419_1', 'Brats18_TCIA03_474_1', 'Brats18_TCIA03_498_1', 'Brats18_TCIA04_111_1', 'Brats18_TCIA04_149_1', 'Brats18_TCIA04_192_1', 'Brats18_TCIA04_328_1', 'Brats18_TCIA04_343_1', 'Brats18_TCIA04_361_1', 'Brats18_TCIA04_437_1', 'Brats18_TCIA04_479_1', 'Brats18_TCIA05_277_1', 'Brats18_TCIA05_396_1', 'Brats18_TCIA05_444_1', 'Brats18_TCIA05_478_1', 'Brats18_TCIA06_165_1', 'Brats18_TCIA06_184_1', 'Brats18_TCIA06_211_1', 'Brats18_TCIA06_247_1', 'Brats18_TCIA06_332_1', 'Brats18_TCIA06_372_1', 'Brats18_TCIA06_409_1', 'Brats18_TCIA06_603_1', 'Brats18_TCIA08_105_1', 'Brats18_TCIA08_113_1', 'Brats18_TCIA08_162_1', 'Brats18_TCIA08_167_1', 'Brats18_TCIA08_205_1', 'Brats18_TCIA08_218_1', 'Brats18_TCIA08_234_1', 'Brats18_TCIA08_242_1', 'Brats18_TCIA08_278_1', 'Brats18_TCIA08_280_1', 'Brats18_TCIA08_319_1', 'Brats18_TCIA08_406_1', 'Brats18_TCIA08_436_1', 'Brats18_TCIA08_469_1']\n",
      "sub_dirs: ['Brats18_2013_0_1', 'Brats18_2013_15_1', 'Brats18_2013_16_1', 'Brats18_2013_1_1', 'Brats18_2013_24_1', 'Brats18_2013_28_1', 'Brats18_2013_29_1', 'Brats18_2013_6_1', 'Brats18_2013_8_1', 'Brats18_2013_9_1', 'Brats18_TCIA09_141_1', 'Brats18_TCIA09_177_1', 'Brats18_TCIA09_254_1', 'Brats18_TCIA09_255_1', 'Brats18_TCIA09_312_1', 'Brats18_TCIA09_402_1', 'Brats18_TCIA09_428_1', 'Brats18_TCIA09_451_1', 'Brats18_TCIA09_462_1', 'Brats18_TCIA09_493_1', 'Brats18_TCIA09_620_1', 'Brats18_TCIA10_103_1', 'Brats18_TCIA10_109_1', 'Brats18_TCIA10_130_1', 'Brats18_TCIA10_152_1', 'Brats18_TCIA10_175_1', 'Brats18_TCIA10_202_1', 'Brats18_TCIA10_241_1', 'Brats18_TCIA10_261_1', 'Brats18_TCIA10_266_1', 'Brats18_TCIA10_276_1', 'Brats18_TCIA10_282_1', 'Brats18_TCIA10_299_1', 'Brats18_TCIA10_307_1', 'Brats18_TCIA10_310_1', 'Brats18_TCIA10_325_1', 'Brats18_TCIA10_330_1', 'Brats18_TCIA10_346_1', 'Brats18_TCIA10_351_1', 'Brats18_TCIA10_387_1', 'Brats18_TCIA10_393_1', 'Brats18_TCIA10_408_1', 'Brats18_TCIA10_410_1', 'Brats18_TCIA10_413_1', 'Brats18_TCIA10_420_1', 'Brats18_TCIA10_442_1', 'Brats18_TCIA10_449_1', 'Brats18_TCIA10_490_1', 'Brats18_TCIA10_625_1', 'Brats18_TCIA10_628_1', 'Brats18_TCIA10_629_1', 'Brats18_TCIA10_632_1', 'Brats18_TCIA10_637_1', 'Brats18_TCIA10_639_1', 'Brats18_TCIA10_640_1', 'Brats18_TCIA10_644_1', 'Brats18_TCIA12_101_1', 'Brats18_TCIA12_249_1', 'Brats18_TCIA12_298_1', 'Brats18_TCIA12_466_1', 'Brats18_TCIA12_470_1', 'Brats18_TCIA12_480_1', 'Brats18_TCIA13_615_1', 'Brats18_TCIA13_618_1', 'Brats18_TCIA13_621_1', 'Brats18_TCIA13_623_1', 'Brats18_TCIA13_624_1', 'Brats18_TCIA13_630_1', 'Brats18_TCIA13_633_1', 'Brats18_TCIA13_634_1', 'Brats18_TCIA13_642_1', 'Brats18_TCIA13_645_1', 'Brats18_TCIA13_650_1', 'Brats18_TCIA13_653_1', 'Brats18_TCIA13_654_1']\n"
     ]
    }
   ],
   "source": [
    "pathhgg_list = file_name_path(bratshgg_path)\n",
    "pathlgg_list = file_name_path(bratslgg_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def normalize(slice, bottom=99, down=1):\n",
    "    \"\"\"\n",
    "    normalize image with mean and std for regionnonzero,and clip the value into range\n",
    "    :param slice:\n",
    "    :param bottom:\n",
    "    :param down:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    #有点像“去掉最低分去掉最高分”的意思,使得数据集更加“公平”\n",
    "    b = np.percentile(slice, bottom)\n",
    "    t = np.percentile(slice, down)\n",
    "    slice = np.clip(slice, t, b)#限定范围numpy.clip(a, a_min, a_max, out=None)\n",
    "\n",
    "    #除了黑色背景外的区域要进行标准化\n",
    "    image_nonzero = slice[np.nonzero(slice)]\n",
    "    if np.std(slice) == 0 or np.std(image_nonzero) == 0:\n",
    "        return slice\n",
    "    else:\n",
    "        tmp = (slice - np.mean(image_nonzero)) / np.std(image_nonzero)\n",
    "        # since the range of intensities is between 0 and 5000 ,\n",
    "        # the min in the normalized slice corresponds to 0 intensity in unnormalized slice\n",
    "        # the min is replaced with -9 just to keep track of 0 intensities\n",
    "        # so that we can discard those intensities afterwards when sampling random patches\n",
    "        tmp[tmp == tmp.min()] = -9 #黑色背景区域\n",
    "        return tmp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.percentil\n",
    "分位数:\n",
    "在我们中学就有过中位数，其实中位数就是一个二分位数，取中位数左边区间的值的概率等于取其右边区间的值的概率。另外在大学的概率论课程中，另一个比较常见的是四分位数，也就是25%,50%,75%这三个切分点。\n",
    "分位数意义：表示了在这个样本集中从小至大排列之后小于某值的样本子集占总样本集的比例\n",
    "我采用了99与1这两个分位数来作为鉴定outliers分布的界限从而进行异常值的修正。这样子的修正可以以更好的准确率来得到我们理想的数据集。\n",
    "有点像去掉最低分去掉最高分的意思"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def crop_ceter(img,croph,cropw):   \n",
    "    #for n_slice in range(img.shape[0]):\n",
    "    height,width = img[0].shape \n",
    "    starth = height//2-(croph//2)\n",
    "    startw = width//2-(cropw//2)        \n",
    "    return img[:,starth:starth+croph,startw:startw+cropw]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Brats18_2013_10_1\n",
      "Brats18_2013_11_1\n",
      "Brats18_2013_12_1\n",
      "Brats18_2013_13_1\n",
      "Brats18_2013_14_1\n",
      "Brats18_2013_17_1\n",
      "Brats18_2013_18_1\n",
      "Brats18_2013_19_1\n",
      "Brats18_2013_20_1\n",
      "Brats18_2013_21_1\n",
      "Brats18_2013_22_1\n",
      "Brats18_2013_23_1\n",
      "Brats18_2013_25_1\n",
      "Brats18_2013_26_1\n",
      "Brats18_2013_27_1\n",
      "Brats18_2013_2_1\n",
      "Brats18_2013_3_1\n",
      "Brats18_2013_4_1\n",
      "Brats18_2013_5_1\n",
      "Brats18_2013_7_1\n",
      "Brats18_CBICA_AAB_1\n",
      "Brats18_CBICA_AAG_1\n",
      "Brats18_CBICA_AAL_1\n",
      "Brats18_CBICA_AAP_1\n",
      "Brats18_CBICA_ABB_1\n",
      "Brats18_CBICA_ABE_1\n",
      "Brats18_CBICA_ABM_1\n",
      "Brats18_CBICA_ABN_1\n",
      "Brats18_CBICA_ABO_1\n",
      "Brats18_CBICA_ABY_1\n",
      "Brats18_CBICA_ALN_1\n",
      "Brats18_CBICA_ALU_1\n",
      "Brats18_CBICA_ALX_1\n",
      "Brats18_CBICA_AME_1\n",
      "Brats18_CBICA_AMH_1\n",
      "Brats18_CBICA_ANG_1\n",
      "Brats18_CBICA_ANI_1\n",
      "Brats18_CBICA_ANP_1\n",
      "Brats18_CBICA_ANZ_1\n",
      "Brats18_CBICA_AOD_1\n",
      "Brats18_CBICA_AOH_1\n",
      "Brats18_CBICA_AOO_1\n",
      "Brats18_CBICA_AOP_1\n",
      "Brats18_CBICA_AOZ_1\n",
      "Brats18_CBICA_APR_1\n",
      "Brats18_CBICA_APY_1\n",
      "Brats18_CBICA_APZ_1\n",
      "Brats18_CBICA_AQA_1\n",
      "Brats18_CBICA_AQD_1\n",
      "Brats18_CBICA_AQG_1\n",
      "Brats18_CBICA_AQJ_1\n",
      "Brats18_CBICA_AQN_1\n",
      "Brats18_CBICA_AQO_1\n",
      "Brats18_CBICA_AQP_1\n",
      "Brats18_CBICA_AQQ_1\n",
      "Brats18_CBICA_AQR_1\n",
      "Brats18_CBICA_AQT_1\n",
      "Brats18_CBICA_AQU_1\n",
      "Brats18_CBICA_AQV_1\n",
      "Brats18_CBICA_AQY_1\n",
      "Brats18_CBICA_AQZ_1\n",
      "Brats18_CBICA_ARF_1\n",
      "Brats18_CBICA_ARW_1\n",
      "Brats18_CBICA_ARZ_1\n",
      "Brats18_CBICA_ASA_1\n",
      "Brats18_CBICA_ASE_1\n",
      "Brats18_CBICA_ASG_1\n",
      "Brats18_CBICA_ASH_1\n",
      "Brats18_CBICA_ASK_1\n",
      "Brats18_CBICA_ASN_1\n",
      "Brats18_CBICA_ASO_1\n",
      "Brats18_CBICA_ASU_1\n",
      "Brats18_CBICA_ASV_1\n",
      "Brats18_CBICA_ASW_1\n",
      "Brats18_CBICA_ASY_1\n",
      "Brats18_CBICA_ATB_1\n",
      "Brats18_CBICA_ATD_1\n",
      "Brats18_CBICA_ATF_1\n",
      "Brats18_CBICA_ATP_1\n",
      "Brats18_CBICA_ATV_1\n",
      "Brats18_CBICA_ATX_1\n",
      "Brats18_CBICA_AUN_1\n",
      "Brats18_CBICA_AUQ_1\n",
      "Brats18_CBICA_AUR_1\n",
      "Brats18_CBICA_AVG_1\n",
      "Brats18_CBICA_AVJ_1\n",
      "Brats18_CBICA_AVV_1\n",
      "Brats18_CBICA_AWG_1\n",
      "Brats18_CBICA_AWH_1\n",
      "Brats18_CBICA_AWI_1\n",
      "Brats18_CBICA_AXJ_1\n",
      "Brats18_CBICA_AXL_1\n",
      "Brats18_CBICA_AXM_1\n",
      "Brats18_CBICA_AXN_1\n",
      "Brats18_CBICA_AXO_1\n",
      "Brats18_CBICA_AXQ_1\n",
      "Brats18_CBICA_AXW_1\n",
      "Brats18_CBICA_AYA_1\n",
      "Brats18_CBICA_AYI_1\n",
      "Brats18_CBICA_AYU_1\n",
      "Brats18_CBICA_AYW_1\n",
      "Brats18_CBICA_AZD_1\n",
      "Brats18_CBICA_AZH_1\n",
      "Brats18_CBICA_BFB_1\n",
      "Brats18_CBICA_BFP_1\n",
      "Brats18_CBICA_BHB_1\n",
      "Brats18_CBICA_BHK_1\n",
      "Brats18_CBICA_BHM_1\n",
      "Brats18_TCIA01_131_1\n",
      "Brats18_TCIA01_147_1\n",
      "Brats18_TCIA01_150_1\n",
      "Brats18_TCIA01_180_1\n",
      "Brats18_TCIA01_186_1\n",
      "Brats18_TCIA01_190_1\n",
      "Brats18_TCIA01_201_1\n",
      "Brats18_TCIA01_203_1\n",
      "Brats18_TCIA01_221_1\n",
      "Brats18_TCIA01_231_1\n",
      "Brats18_TCIA01_235_1\n",
      "Brats18_TCIA01_335_1\n",
      "Brats18_TCIA01_378_1\n",
      "Brats18_TCIA01_390_1\n",
      "Brats18_TCIA01_401_1\n",
      "Brats18_TCIA01_411_1\n",
      "Brats18_TCIA01_412_1\n",
      "Brats18_TCIA01_425_1\n",
      "Brats18_TCIA01_429_1\n",
      "Brats18_TCIA01_448_1\n",
      "Brats18_TCIA01_460_1\n",
      "Brats18_TCIA01_499_1\n",
      "Brats18_TCIA02_117_1\n",
      "Brats18_TCIA02_118_1\n",
      "Brats18_TCIA02_135_1\n",
      "Brats18_TCIA02_151_1\n",
      "Brats18_TCIA02_168_1\n",
      "Brats18_TCIA02_171_1\n",
      "Brats18_TCIA02_179_1\n",
      "Brats18_TCIA02_198_1\n",
      "Brats18_TCIA02_208_1\n",
      "Brats18_TCIA02_222_1\n",
      "Brats18_TCIA02_226_1\n",
      "Brats18_TCIA02_274_1\n",
      "Brats18_TCIA02_283_1\n",
      "Brats18_TCIA02_290_1\n",
      "Brats18_TCIA02_300_1\n",
      "Brats18_TCIA02_309_1\n",
      "Brats18_TCIA02_314_1\n",
      "Brats18_TCIA02_321_1\n",
      "Brats18_TCIA02_322_1\n",
      "Brats18_TCIA02_331_1\n",
      "Brats18_TCIA02_368_1\n",
      "Brats18_TCIA02_370_1\n",
      "Brats18_TCIA02_374_1\n",
      "Brats18_TCIA02_377_1\n",
      "Brats18_TCIA02_394_1\n",
      "Brats18_TCIA02_430_1\n",
      "Brats18_TCIA02_455_1\n",
      "Brats18_TCIA02_471_1\n",
      "Brats18_TCIA02_473_1\n",
      "Brats18_TCIA02_491_1\n",
      "Brats18_TCIA02_605_1\n",
      "Brats18_TCIA02_606_1\n",
      "Brats18_TCIA02_607_1\n",
      "Brats18_TCIA02_608_1\n",
      "Brats18_TCIA03_121_1\n",
      "Brats18_TCIA03_133_1\n",
      "Brats18_TCIA03_138_1\n",
      "Brats18_TCIA03_199_1\n",
      "Brats18_TCIA03_257_1\n",
      "Brats18_TCIA03_265_1\n",
      "Brats18_TCIA03_296_1\n",
      "Brats18_TCIA03_338_1\n",
      "Brats18_TCIA03_375_1\n",
      "Brats18_TCIA03_419_1\n",
      "Brats18_TCIA03_474_1\n",
      "Brats18_TCIA03_498_1\n",
      "Brats18_TCIA04_111_1\n",
      "Brats18_TCIA04_149_1\n",
      "Brats18_TCIA04_192_1\n",
      "Brats18_TCIA04_328_1\n",
      "Brats18_TCIA04_343_1\n",
      "Brats18_TCIA04_361_1\n",
      "Brats18_TCIA04_437_1\n",
      "Brats18_TCIA04_479_1\n",
      "Brats18_TCIA05_277_1\n",
      "Brats18_TCIA05_396_1\n",
      "Brats18_TCIA05_444_1\n",
      "Brats18_TCIA05_478_1\n",
      "Brats18_TCIA06_165_1\n",
      "Brats18_TCIA06_184_1\n",
      "Brats18_TCIA06_211_1\n",
      "Brats18_TCIA06_247_1\n",
      "Brats18_TCIA06_332_1\n",
      "Brats18_TCIA06_372_1\n",
      "Brats18_TCIA06_409_1\n",
      "Brats18_TCIA06_603_1\n",
      "Brats18_TCIA08_105_1\n",
      "Brats18_TCIA08_113_1\n",
      "Brats18_TCIA08_162_1\n",
      "Brats18_TCIA08_167_1\n",
      "Brats18_TCIA08_205_1\n",
      "Brats18_TCIA08_218_1\n",
      "Brats18_TCIA08_234_1\n",
      "Brats18_TCIA08_242_1\n",
      "Brats18_TCIA08_278_1\n",
      "Brats18_TCIA08_280_1\n",
      "Brats18_TCIA08_319_1\n",
      "Brats18_TCIA08_406_1\n",
      "Brats18_TCIA08_436_1\n",
      "Brats18_TCIA08_469_1\n"
     ]
    }
   ],
   "source": [
    "for subsetindex in range(len(pathhgg_list)):\n",
    "    brats_subset_path = bratshgg_path + \"/\" + str(pathhgg_list[subsetindex]) + \"/\"\n",
    "    #获取每个病例的四个模态及Mask的路径\n",
    "    flair_image = brats_subset_path + str(pathhgg_list[subsetindex]) + flair_name\n",
    "    t1_image = brats_subset_path + str(pathhgg_list[subsetindex]) + t1_name\n",
    "    t1ce_image = brats_subset_path + str(pathhgg_list[subsetindex]) + t1ce_name\n",
    "    t2_image = brats_subset_path + str(pathhgg_list[subsetindex]) + t2_name\n",
    "    mask_image = brats_subset_path + str(pathhgg_list[subsetindex]) + mask_name\n",
    "    #获取每个病例的四个模态及Mask数据\n",
    "    flair_src = sitk.ReadImage(flair_image, sitk.sitkInt16)\n",
    "    t1_src = sitk.ReadImage(t1_image, sitk.sitkInt16)\n",
    "    t1ce_src = sitk.ReadImage(t1ce_image, sitk.sitkInt16)\n",
    "    t2_src = sitk.ReadImage(t2_image, sitk.sitkInt16)\n",
    "    mask = sitk.ReadImage(mask_image, sitk.sitkUInt8)\n",
    "    #GetArrayFromImage()可用于将SimpleITK对象转换为ndarray\n",
    "    flair_array = sitk.GetArrayFromImage(flair_src)\n",
    "    t1_array = sitk.GetArrayFromImage(t1_src)\n",
    "    t1ce_array = sitk.GetArrayFromImage(t1ce_src)\n",
    "    t2_array = sitk.GetArrayFromImage(t2_src)\n",
    "    mask_array = sitk.GetArrayFromImage(mask)\n",
    "    #对四个模态分别进行标准化,由于它们对比度不同\n",
    "    flair_array_nor = normalize(flair_array)\n",
    "    t1_array_nor = normalize(t1_array)\n",
    "    t1ce_array_nor = normalize(t1ce_array)\n",
    "    t2_array_nor = normalize(t2_array)\n",
    "    #裁剪(偶数才行)\n",
    "    flair_crop = crop_ceter(flair_array_nor,160,160)\n",
    "    t1_crop = crop_ceter(t1_array_nor,160,160)\n",
    "    t1ce_crop = crop_ceter(t1ce_array_nor,160,160)\n",
    "    t2_crop = crop_ceter(t2_array_nor,160,160)\n",
    "    mask_crop = crop_ceter(mask_array,160,160) \n",
    "    print(str(pathhgg_list[subsetindex]))\n",
    "    #切片处理,并去掉没有病灶的切片\n",
    "    for n_slice in range(flair_crop.shape[0]):\n",
    "        if np.max(mask_crop[n_slice,:,:]) != 0:\n",
    "            maskImg = mask_crop[n_slice,:,:]\n",
    "            \n",
    "            FourModelImageArray = np.zeros((flair_crop.shape[1],flair_crop.shape[2],4),np.float)\n",
    "            flairImg = flair_crop[n_slice,:,:]\n",
    "            flairImg = flairImg.astype(np.float)\n",
    "            FourModelImageArray[:,:,0] = flairImg\n",
    "            t1Img = t1_crop[n_slice,:,:]\n",
    "            t1Img = t1Img.astype(np.float)\n",
    "            FourModelImageArray[:,:,1] = t1Img\n",
    "            t1ceImg = t1ce_crop[n_slice,:,:]\n",
    "            t1ceImg = t1ceImg.astype(np.float)\n",
    "            FourModelImageArray[:,:,2] = t1ceImg\n",
    "            t2Img = t2_crop[n_slice,:,:]\n",
    "            t2Img = t2Img.astype(np.float)\n",
    "            FourModelImageArray[:,:,3] = t2Img       \n",
    "        \n",
    "            imagepath = outputImg_path + \"\\\\\" + str(pathhgg_list[subsetindex]) + \"_\" + str(n_slice) + \".npy\"\n",
    "            maskpath = outputMask_path + \"\\\\\" + str(pathhgg_list[subsetindex]) + \"_\" + str(n_slice) + \".npy\"\n",
    "            np.save(imagepath,FourModelImageArray)#(160,160,4) np.float dtype('float64')\n",
    "            np.save(maskpath,maskImg)# (160, 160) dtype('uint8') 值为0 1 2 4\n",
    "print(\"Done！\")\n",
    "        \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Brats18_2013_0_1\n",
      "Brats18_2013_15_1\n",
      "Brats18_2013_16_1\n",
      "Brats18_2013_1_1\n",
      "Brats18_2013_24_1\n",
      "Brats18_2013_28_1\n",
      "Brats18_2013_29_1\n",
      "Brats18_2013_6_1\n",
      "Brats18_2013_8_1\n",
      "Brats18_2013_9_1\n",
      "Brats18_TCIA09_141_1\n",
      "Brats18_TCIA09_177_1\n",
      "Brats18_TCIA09_254_1\n",
      "Brats18_TCIA09_255_1\n",
      "Brats18_TCIA09_312_1\n",
      "Brats18_TCIA09_402_1\n",
      "Brats18_TCIA09_428_1\n",
      "Brats18_TCIA09_451_1\n",
      "Brats18_TCIA09_462_1\n",
      "Brats18_TCIA09_493_1\n",
      "Brats18_TCIA09_620_1\n",
      "Brats18_TCIA10_103_1\n",
      "Brats18_TCIA10_109_1\n",
      "Brats18_TCIA10_130_1\n",
      "Brats18_TCIA10_152_1\n",
      "Brats18_TCIA10_175_1\n",
      "Brats18_TCIA10_202_1\n",
      "Brats18_TCIA10_241_1\n",
      "Brats18_TCIA10_261_1\n",
      "Brats18_TCIA10_266_1\n",
      "Brats18_TCIA10_276_1\n",
      "Brats18_TCIA10_282_1\n",
      "Brats18_TCIA10_299_1\n",
      "Brats18_TCIA10_307_1\n",
      "Brats18_TCIA10_310_1\n",
      "Brats18_TCIA10_325_1\n",
      "Brats18_TCIA10_330_1\n",
      "Brats18_TCIA10_346_1\n",
      "Brats18_TCIA10_351_1\n",
      "Brats18_TCIA10_387_1\n",
      "Brats18_TCIA10_393_1\n",
      "Brats18_TCIA10_408_1\n",
      "Brats18_TCIA10_410_1\n",
      "Brats18_TCIA10_413_1\n",
      "Brats18_TCIA10_420_1\n",
      "Brats18_TCIA10_442_1\n",
      "Brats18_TCIA10_449_1\n",
      "Brats18_TCIA10_490_1\n",
      "Brats18_TCIA10_625_1\n",
      "Brats18_TCIA10_628_1\n",
      "Brats18_TCIA10_629_1\n",
      "Brats18_TCIA10_632_1\n",
      "Brats18_TCIA10_637_1\n",
      "Brats18_TCIA10_639_1\n",
      "Brats18_TCIA10_640_1\n",
      "Brats18_TCIA10_644_1\n",
      "Brats18_TCIA12_101_1\n",
      "Brats18_TCIA12_249_1\n",
      "Brats18_TCIA12_298_1\n",
      "Brats18_TCIA12_466_1\n",
      "Brats18_TCIA12_470_1\n",
      "Brats18_TCIA12_480_1\n",
      "Brats18_TCIA13_615_1\n",
      "Brats18_TCIA13_618_1\n",
      "Brats18_TCIA13_621_1\n",
      "Brats18_TCIA13_623_1\n",
      "Brats18_TCIA13_624_1\n",
      "Brats18_TCIA13_630_1\n",
      "Brats18_TCIA13_633_1\n",
      "Brats18_TCIA13_634_1\n",
      "Brats18_TCIA13_642_1\n",
      "Brats18_TCIA13_645_1\n",
      "Brats18_TCIA13_650_1\n",
      "Brats18_TCIA13_653_1\n",
      "Brats18_TCIA13_654_1\n",
      "Done!\n"
     ]
    }
   ],
   "source": [
    "for subsetindex in range(len(pathlgg_list)):\n",
    "    brats_subset_path = bratslgg_path + \"/\" + str(pathlgg_list[subsetindex]) + \"/\"\n",
    "    #获取每个病例的四个模态及Mask的路径\n",
    "    flair_image = brats_subset_path + str(pathlgg_list[subsetindex]) + flair_name\n",
    "    t1_image = brats_subset_path + str(pathlgg_list[subsetindex]) + t1_name\n",
    "    t1ce_image = brats_subset_path + str(pathlgg_list[subsetindex]) + t1ce_name\n",
    "    t2_image = brats_subset_path + str(pathlgg_list[subsetindex]) + t2_name\n",
    "    mask_image = brats_subset_path + str(pathlgg_list[subsetindex]) + mask_name\n",
    "    #获取每个病例的四个模态及Mask数据\n",
    "    flair_src = sitk.ReadImage(flair_image, sitk.sitkInt16)\n",
    "    t1_src = sitk.ReadImage(t1_image, sitk.sitkInt16)\n",
    "    t1ce_src = sitk.ReadImage(t1ce_image, sitk.sitkInt16)\n",
    "    t2_src = sitk.ReadImage(t2_image, sitk.sitkInt16)\n",
    "    mask = sitk.ReadImage(mask_image, sitk.sitkUInt8)\n",
    "    #GetArrayFromImage()可用于将SimpleITK对象转换为ndarray\n",
    "    flair_array = sitk.GetArrayFromImage(flair_src)\n",
    "    t1_array = sitk.GetArrayFromImage(t1_src)\n",
    "    t1ce_array = sitk.GetArrayFromImage(t1ce_src)\n",
    "    t2_array = sitk.GetArrayFromImage(t2_src)\n",
    "    mask_array = sitk.GetArrayFromImage(mask)\n",
    "    #对四个模态分别进行标准化,由于它们对比度不同\n",
    "    flair_array_nor = normalize(flair_array)\n",
    "    t1_array_nor = normalize(t1_array)\n",
    "    t1ce_array_nor = normalize(t1ce_array)\n",
    "    t2_array_nor = normalize(t2_array)\n",
    "    #裁剪(偶数才行)\n",
    "    flair_crop = crop_ceter(flair_array_nor,160,160)\n",
    "    t1_crop = crop_ceter(t1_array_nor,160,160)\n",
    "    t1ce_crop = crop_ceter(t1ce_array_nor,160,160)\n",
    "    t2_crop = crop_ceter(t2_array_nor,160,160)\n",
    "    mask_crop = crop_ceter(mask_array,160,160) \n",
    "    print(str(pathlgg_list[subsetindex]))\n",
    "    #切片处理,并去掉没有病灶的切片\n",
    "    for n_slice in range(flair_crop.shape[0]):\n",
    "        if np.max(mask_crop[n_slice,:,:]) != 0:\n",
    "            maskImg = mask_crop[n_slice,:,:]\n",
    "            \n",
    "            FourModelImageArray = np.zeros((flair_crop.shape[1],flair_crop.shape[2],4),np.float)\n",
    "            flairImg = flair_crop[n_slice,:,:]\n",
    "            flairImg = flairImg.astype(np.float)\n",
    "            FourModelImageArray[:,:,0] = flairImg\n",
    "            t1Img = t1_crop[n_slice,:,:]\n",
    "            t1Img = t1Img.astype(np.float)\n",
    "            FourModelImageArray[:,:,1] = t1Img\n",
    "            t1ceImg = t1ce_crop[n_slice,:,:]\n",
    "            t1ceImg = t1ceImg.astype(np.float)\n",
    "            FourModelImageArray[:,:,2] = t1ceImg\n",
    "            t2Img = t2_crop[n_slice,:,:]\n",
    "            t2Img = t2Img.astype(np.float)\n",
    "            FourModelImageArray[:,:,3] = t2Img       \n",
    "        \n",
    "            imagepath = outputImg_path + \"\\\\\" + str(pathlgg_list[subsetindex]) + \"_\" + str(n_slice) + \".npy\"\n",
    "            maskpath = outputMask_path + \"\\\\\" + str(pathlgg_list[subsetindex]) + \"_\" + str(n_slice) + \".npy\"\n",
    "            np.save(imagepath,FourModelImageArray)#(160,160,4) np.float dtype('float64')\n",
    "            np.save(maskpath,maskImg)# (160, 160) dtype('uint8') 值为0 1 2 4\n",
    "print(\"Done!\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
